{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "library(oce)  # 加载这个包用来计算太阳仰角以及离太阳的距离\n",
    "library(plyr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "add_solar = function(data, longitude, latitude)\n",
    "{\n",
    "  cal_4_solar = function(time_str)\n",
    "  {\n",
    "    # 计算出太阳仰角， 离太阳的距离, 并根据大气折射进行校正\n",
    "    result = sunAngle(as.POSIXct(time_str, tz=\"Asia/Taipei\"), \n",
    "                      longitude = longitude, latitude = latitude, \n",
    "                      useRefraction = TRUE)\n",
    "    \n",
    "    # 计算出年份，月份，天数等等，作为特征\n",
    "    time_list = as.POSIXlt(time_str, tz=\"Asia/Taipei\")\n",
    "    \n",
    "    year = time_list$year\n",
    "    month = time_list$mon + 1  # 月份居然从0开始\n",
    "    yday = time_list$yday\n",
    "    mday = time_list$mday\n",
    "    hour = time_list$hour\n",
    "    minute = time_list$min\n",
    "    second = time_list$sec\n",
    "    hours_float = hour + minute/60 + second/3600\n",
    "    \n",
    "    # 把计算出的结果统一存到result这个list里，然后返回\n",
    "    result$time = NULL  # 这个浮点日期没啥意义，删掉\n",
    "    result$hours_float = hours_float\n",
    "    result$mday = mday\n",
    "    result$yday = yday\n",
    "    result$month = month\n",
    "    result$year = year\n",
    "    \n",
    "    return(result)\n",
    "  }\n",
    "  \n",
    "  # 把计算出的结果存一个dataframe\n",
    "  pre_df = t(sapply(data$时间, cal_4_solar))\n",
    "  \n",
    "  solar_df = data.frame(matrix(unlist(pre_df), nrow=dim(pre_df)[1], ncol=dim(pre_df)[2], byrow=FALSE))\n",
    "  colnames(solar_df) = colnames(pre_df)\n",
    "  # 把计算出的特征dataframe和原始的dataframe合并起来, 然后返回\n",
    "  cbind_df = cbind(data, solar_df)\n",
    "  \n",
    "  return(cbind_df)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "latitude_list=list(36.3329,43.0287,36.3329,36.3329,43.0287,36.3329,43.0287,36.3329,36.6207,43.8016)\n",
    "longtude_list=list(97.1849,92.6784,97.1849,97.1849,92.6784,97.1849,92.6784,97.1849,101.825,87.5967)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for(i in 1:10){\n",
    "    setwd(' ')\n",
    "    sprintf('正在处理文件 %d',i) \n",
    "    file_name=paste('test_',i,'.csv',sep = \"\")\n",
    "    train = read.csv(file_name, fileEncoding = 'UTF-8')\n",
    "    longitude=longtude_list[[i]]\n",
    "    latitude=latitude_list[[i]]\n",
    "    train_new=add_solar(train,longitude,latitude)\n",
    "    setwd('')\n",
    "    new_file=paste('test_new_',i,'.csv',sep=\"\")\n",
    "    write.csv(train_new, new_file, row.names = FALSE, fileEncoding = 'GBK')}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
